package com.cqjtu.store.product.controller;

import com.cqjtu.store.common.JwtUtils;
import com.cqjtu.store.common.PageVO;
import com.cqjtu.store.common.R;
import com.cqjtu.store.product.pojo.dto.ProductCategoryPageDTO;
import com.cqjtu.store.product.pojo.dto.ProductDTO;
import com.cqjtu.store.product.pojo.dto.ProductPageDTO;
import com.cqjtu.store.product.pojo.entity.Product;
import com.cqjtu.store.product.pojo.vo.ProductDetailVO;
import com.cqjtu.store.product.pojo.vo.ProductListVO;
import com.cqjtu.store.product.pojo.vo.ProductVO;
import com.cqjtu.store.product.service.ProductService;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ProjectName: cqjtu-store
 * @Titile: ProductController
 * @Author: Lucky
 * @Description: 商品控制类
 */
@RestController
@RequestMapping("api/product")
@Api(tags = "商品模块")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 新到好货
     * @return
     */
    @ApiOperation("查询新到好货")
    @GetMapping("new")
    public R<List<ProductVO>> getNewList(){
        List<ProductVO> newList = productService.getNewList();
        return R.ok(newList);
    }
    /**
     * 热销排行
     * @return
     */
    @ApiOperation("查询热销商品")
    @GetMapping("hot")
    public R<List<ProductVO>> getHotList(){
        List<ProductVO> hotList = productService.getHotList();
        return R.ok(hotList);
    }

    @GetMapping("list")
    @ApiOperation("根据种类id分页获取商品列表")
    public R<PageVO<List<ProductListVO>>> getListByCategoryId(@RequestParam("pageIndex") Integer pageIndex,
                                                              @RequestParam("pageSize") Integer pageSize,
                                                              @RequestParam("categoryId") Integer categoryId,
                                                              HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = null;
        if(token != null && token!= ""){
            //已经登录
            userLoginVO = JwtUtils.parseToken(token);
        }
        ProductCategoryPageDTO categoryPageDTO = new ProductCategoryPageDTO();
        categoryPageDTO.setCategoryId(categoryId);
        categoryPageDTO.setPageSize(pageSize);
        categoryPageDTO.setPageIndex(pageIndex);
        categoryPageDTO.setOffset();
        PageVO<List<ProductListVO>> pageVO = productService.getListByCategoryId(categoryPageDTO,userLoginVO);
        return R.ok(pageVO);
    }


    @ApiOperation("PageHelper获取商品列表")
    @PostMapping("listByPageHelper")
    public R<PageInfo<Product>> getByPage(@RequestBody ProductPageDTO categoryPageDTO){
        PageInfo<Product> productVOList = productService.getByPage(categoryPageDTO);
        return R.ok(productVOList);
    }

    /**
     * 自定义分页
     * @param categoryPageDTO
     * @return
     */
    @ApiOperation("自定义分页获取商品列表")
    @GetMapping("list3")
    public R<PageVO<List<ProductVO>>> getListByCategoryId3(@RequestBody ProductCategoryPageDTO categoryPageDTO){
        PageVO<List<ProductVO>> listPageVO = productService.getListByCategoryId3(categoryPageDTO);
        return R.ok(listPageVO);
    }

    /**
     * 查询商品详情
     * @param id
     * @param request
     * @return
     */
    @ApiOperation("根据id查询商品详情")
    @GetMapping("{id}")
    public R<ProductDetailVO> getById(@PathVariable Integer id, HttpServletRequest request){
        String token = request.getHeader("Authorization");

        UserLoginVO userLoginVO = null;
        if(token != null && token!=""){
            //已经登录
            userLoginVO = JwtUtils.parseToken(token);
        }
        ProductDetailVO detailVO = productService.getById(id,userLoginVO);
        return R.ok(detailVO);
    }
    @ApiOperation("添加商品信息")
    @PostMapping("add")
    public R<Void> add(@RequestBody @Validated ProductDTO productDTO, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        productService.addProduct(productDTO,userLoginVO);
        return R.ok();
    }

    @ApiOperation("修改商品信息")
    @PutMapping("update")
    public R<Void> update(@RequestBody @Validated ProductDTO productDTO, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        productService.updateProduct(productDTO,userLoginVO);
        return R.ok();
    }

    @ApiOperation("删除商品信息")
    @DeleteMapping("delete")
    public R<Void> delete(@RequestParam Integer productId, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        productService.deleteProduct(productId);
        return R.ok();
    }

}
